//
//  WeiboHelper.m
//  ShareTest
//
//  Created by Eric Wang on 15/2/2.
//  Copyright (c) 2015年 LinkMob. All rights reserved.
//

#import "WeiboHelper.h"
#import "WeiboMacro.h"


@implementation WeiboHelper

+ (instancetype)shareInstance {
    
    static WeiboHelper *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
    }
    
    return self;
}

- (void)SSOLogin {
    
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = kWeiboRedirectURI;
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
    
//    NSLog(@"response %@", response.userInfo);
//    _accessToken = [response.userInfo objectForKey:@"access_token"];
//    
    if ([self.delegate respondsToSelector:@selector(WeiboResponse:)]) {
        [self.delegate WeiboResponse:response];
    }
    


}

- (void)shareTitle:(NSString *)title
        localImage:(NSData *)imageData
       remoteImage:(NSString *)imageUrl {
    
    // 可能需要添加额外的视图
    if (self.accessToken) {
        [WBHttpRequest requestForShareAStatus:@"发布微博测试" contatinsAPicture:nil orPictureUrl:@"http://ww2.sinaimg.cn/large/694c568fjw1eou7fqd2a9j20yi1pc7wh.jpg" withAccessToken:self.accessToken andOtherProperties:nil queue:nil withCompletionHandler:^(WBHttpRequest *httpRequest, id result, NSError *error) {
            NSLog(@"result %@", result);
        }];
    }
    else {
        WBAuthorizeRequest *request = [WBAuthorizeRequest request];
        request.redirectURI = kWeiboRedirectURI;
        request.scope = @"all";
        request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                             @"Other_Info_1": [NSNumber numberWithInt:123],
                             @"Other_Info_2": @[@"obj1", @"obj2"],
                             @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
        [WeiboSDK sendRequest:request];
    }
    
}

#pragma mark - WBHttpRequestDelegate
- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"收到网络回调" message:[NSString stringWithFormat:@"%@", result] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alertView show];
}

- (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请求异常" message:[NSString stringWithFormat:@"%@", error] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alertView show];
}

-(void)didReceiveWeiboRequest:(WBBaseRequest *)request{
    NSLog(@"weibo request:%@",request);
}

@end
